export type PromiseCallBack = (...args: any) => void;
export type PromiseFn = (resolve: PromiseCallBack, reject?: PromiseCallBack) => void;

// abort
export type AbortFn = {
    (): void;
    // 定义额外的属性
    _reject: PromiseCallBack | null;
};

function getPromise<T>(cb: PromiseFn) {
    let _rej: PromiseCallBack | null = null;
    const promise = new Promise<T>((res, rej) => {
        _rej = rej;
        cb(res, rej);
    });
    const abort = (() => {
        if (_rej) {
            _rej({
                errMsg: "request:fail abort",
                aborted: true,
            });
        }
    }) as AbortFn;
    abort._reject = _rej;

    return {
        promise,
        abort,
    };
}

export { getPromise };
